#!/usr/bin/env python
#
# Start a bed monitoring subsystem.
#

import monitor.collector
from monitor.collector import DataCollector
import monitor.vitals
from monitor.vitals import VitalsBuffer
import monitor.alarms
from monitor.alarms import AlarmChecker
import argparse
import time

if __name__ == '__main__':

	parser = argparse.ArgumentParser(description="Start the Bed Monitor Data Collector.")
	parser.add_argument("bed_id", type=str, help="Bed subsystem identifier")
	parser.add_argument("nurse_port", type=int, help="Port to use to connect to Nurse's station")
	parser.add_argument("port", type=int, help="Beginning of port range to use for bed subsystem communication.")
	args = parser.parse_args()

	collector = DataCollector(args.bed_id, 5)
	vitals_buffer = VitalsBuffer(args.bed_id)
	alarms_checker = AlarmChecker(args.bed_id)

	vitals_buffer.addListener(args.port) # from data collector
	collector.addConnector(args.port) # to vital buffer
	alarms_checker.addListener(args.port + 1) # from data collector
	collector.addConnector(args.port + 1) # to alarm system

	alarms_checker.addConnector(args.port + 3) # to bedside ui
	collector.addConnector(args.port + 2) # to bedside ui
	collector.addConnector(args.nurse_port) # nurse station
	alarms_checker.addConnector(args.nurse_port + 1)

	collector.start()
	alarms_checker.start()
	vitals_buffer.start()

	collector.register(vitals_buffer.id)
	collector.register(alarms_checker.id)
	collector.register("VITALUI_" + args.bed_id)
	collector.register("NURSE_1")
	collector.updateLoop()


	time.sleep(1)
	alarms_checker.register("VITALUI_" + args.bed_id)
	alarms_checker.register("NURSE_1")
	alarms_checker.updateLoop()